package uk.co.droidinactu.sony.prst1.home.view;

import java.io.File;
import java.util.Date;
import java.util.List;

import uk.co.droidinactu.common.file.AsciiFileReader;
import uk.co.droidinactu.sony.prst1.home.R;
import uk.co.droidinactu.sony.prst1.home.SonyPrsT1HomeActivity;
import uk.co.droidinactu.sony.prst1.home.data.SonyDataModel;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public final class Page2Activity extends AbstractActivity implements OnClickListener {

    private static final int DIALOG_NOT_IMPLEMENTED_ID = 16384;
    private Button btnAllNotes;
    private Button btnApps;
    private Button btnAudio;
    private Button btnBooks;
    private Button btnBrowser;
    private Button btnCollections;
    private Button btnDictionary;
    private Button btnGoogleBooks;
    private Button btnHandwriting;
    private Button btnPeriodicals;
    private Button btnPictures;
    private ImageButton btnPrevPage;

    private Button btnPubliclibrary;

    private Button btnPurchasedContent;

    private Button btnSonySettings;
    private Button btnTextMemo;
    private TextView lblClock;
    private SonyDataModel sonyDataModel;

    @Override
    public void onClick(final View v) {
        if (v.getTag().equals("apps")) {
            this.startActivity(new Intent(this.getApplication(), ApplicationsActivity.class));
        } else if (v.getTag().equals("collections")) {
            this.startActivity(new Intent(this.getApplication(), CollectionsActivity.class));
        } else if (v.getTag().equals("books")) {
            this.startActivity(new Intent(this.getApplication(), BooksActivity.class));
        } else if (v.getTag().equals("periodicals")) {
            this.startActivity(new Intent(this.getApplication(), PeriodicalsActivity.class));
        } else if (v.getTag().equals("public library")) {
            final Intent launchIntent = this.getPackageManager()
                    .getLaunchIntentForPackage("com.sony.drbd.ebook.netapp");
            // this.startActivity(launchIntent);
            final String url = "http://sonysearch.overdrive.com/";
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            this.startActivity(i);

        } else if (v.getTag().equals("google books")) {
            final String url = "http://books.google.com/";
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            this.startActivity(i);

        } else if (v.getTag().equals("browser")) {
            final String url = "http://www.google.co.uk/";
            final Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            this.startActivity(i);

        } else if (v.getTag().equals("purchased content")) {
            final Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage(
                    "com.sony.drbd.ebook.storebrowser");
            this.startActivity(launchIntent);

        } else if (v.getTag().equals("all notes")) {
            // this.startActivity(new Intent(this.getApplication(),
            // AllNotesActivity.class));
            this.showNotImplementedDialog();

        } else if (v.getTag().equals("dictionary")) {
            Intent launchIntent = new Intent(Intent.ACTION_MAIN);
            launchIntent = this.getPackageManager().getLaunchIntentForPackage("com.sony.drbd.ebook.Dictionary");
            this.startActivity(launchIntent);
            // this.showNotImplementedDialog();

        } else if (v.getTag().equals("handwriting")) {
            // this.startActivity(new Intent(this.getApplication(),
            // HandWritingActivity.class));
            this.showNotImplementedDialog();

        } else if (v.getTag().equals("text memo")) {
            // this.startActivity(new Intent(this.getApplication(),
            // TextMemoActivity.class));
            this.showNotImplementedDialog();

        } else if (v.getTag().equals("prevpage")) {
            this.finish();
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.page2);

        this.lblClock = (TextView) this.findViewById(R.id.page2_text_clock);

        this.btnPrevPage = (ImageButton) this.findViewById(R.id.page2_btn_prevpage);
        this.btnPrevPage.setOnClickListener(this);

        this.btnPubliclibrary = (Button) this.findViewById(R.id.page2_btn_public_library);
        this.btnPubliclibrary.setOnClickListener(this);

        this.btnGoogleBooks = (Button) this.findViewById(R.id.page2_btn_google_books);
        this.btnGoogleBooks.setOnClickListener(this);

        this.btnBrowser = (Button) this.findViewById(R.id.page2_btn_browser);
        this.btnBrowser.setOnClickListener(this);

        this.btnPurchasedContent = (Button) this.findViewById(R.id.page2_btn_purchased_content);
        this.btnPurchasedContent.setOnClickListener(this);

        this.btnAllNotes = (Button) this.findViewById(R.id.page2_btn_all_notes);
        this.btnAllNotes.setOnClickListener(this);

        this.btnDictionary = (Button) this.findViewById(R.id.page2_btn_dictionary);
        this.btnDictionary.setOnClickListener(this);

        this.btnHandwriting = (Button) this.findViewById(R.id.page2_btn_handwriting);
        this.btnHandwriting.setOnClickListener(this);

        this.btnTextMemo = (Button) this.findViewById(R.id.page2_btn_text_memo);
        this.btnTextMemo.setOnClickListener(this);

        this.btnPeriodicals = (Button) this.findViewById(R.id.page2_btn_periodicals);
        this.btnPeriodicals.setOnClickListener(this);

        this.btnCollections = (Button) this.findViewById(R.id.page2_btn_collections);
        this.btnCollections.setOnClickListener(this);

        this.btnApps = (Button) this.findViewById(R.id.page2_btn_apps);
        this.btnApps.setOnClickListener(this);

        this.btnBooks = (Button) this.findViewById(R.id.page2_btn_books);
        this.btnBooks.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, SonyPrsT1HomeActivity.MENU_SETTINGS, 0, R.string.menu_settings).setIcon(
                android.R.drawable.ic_menu_preferences);
        menu.add(0, SonyPrsT1HomeActivity.MENU_PREFERENCES, 0, R.string.menu_preferences).setIcon(
                android.R.drawable.ic_menu_preferences);
        menu.add(0, SonyPrsT1HomeActivity.MENU_CLEAR_DEFAULTS, 0, R.string.menu_clear_default_settings).setIcon(
                android.R.drawable.ic_menu_close_clear_cancel);
        menu.add(0, SonyPrsT1HomeActivity.MENU_ABOUT, 0, R.string.menu_about).setIcon(R.drawable.ic_menu_about);

        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (this.sonyDataModel == null) {
            this.sonyDataModel = new SonyDataModel(this.getApplication());
        }

        long nbrBooks = 0;
        long nbrCollections = 0;
        long nbrPeriodicals = 0;
        Cursor tmpCursor = this.sonyDataModel.getBooks(this.getApplication(), BookSortBy.TITLE);
        if (tmpCursor != null) {
            nbrBooks = tmpCursor.getCount();
            tmpCursor.close();
        }
        tmpCursor = this.sonyDataModel.getCollections(0, 9999, null);
        if (tmpCursor != null) {
            nbrCollections = tmpCursor.getCount();
            tmpCursor.close();
        }
        tmpCursor = this.sonyDataModel.getPeriodicals(this.getApplication());
        if (tmpCursor != null) {
            nbrPeriodicals = tmpCursor.getCount();
            tmpCursor.close();
        }

        final PackageManager manager = this.getPackageManager();
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);

        this.btnApps.setText(String.format(this.getResources().getString(R.string.page1_Apps), apps.size()));
        this.btnBooks.setText(String.format(this.getResources().getString(R.string.page1_Books), nbrBooks));
        this.btnCollections.setText(String.format(this.getResources().getString(R.string.page1_Collections),
                nbrCollections));
        this.btnPeriodicals.setText(String.format(this.getResources().getString(R.string.page1_Periodicals),
                nbrPeriodicals));

        final File notesDir = new File("/mnt/sdcard/Sony_Reader/media/notepads");
        final String[] notes = notesDir.list();
        final AsciiFileReader afr = new AsciiFileReader();
        String line;
        int drawings = 0;
        int text = 0;
        for (final String f : notes) {
            afr.setFilename("/mnt/sdcard/Sony_Reader/media/notepads/" + f);
            line = afr.firstLineFromFile();
            if (line.contains("notepad type=\"drawing\"")) {
                drawings++;
            } else if (line.contains("notepad type=\"text\"")) {
                text++;
            }
            afr.closeFile();
        }
        this.btnAllNotes.setText(String.format(this.getResources().getString(R.string.page2_All_Notes), 0));// notes.length));
        this.btnTextMemo.setText(String.format(this.getResources().getString(R.string.page2_Text_Memo), text));
        this.btnHandwriting.setText(String.format(this.getResources().getString(R.string.page2_Handwriting), drawings));

        this.updateCurrentTime();
    }

    private void updateCurrentTime() {
        this.lblClock.setText(DateFormat.format("kk:mm:ss", new Date()));
    }
}
